﻿#if DEBUG_GRAPHICS_ENABLED

using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SunXNA.RainbowXNA.Batch;

namespace SunXNA.RainbowXNA.RenderObject
{
    class CVertexRenderObject  : IBaseRenderObject
    {
        private VertexPositionColor[] m_verts;
        private VertexDeclaration m_vertexDeclaration;
        private PrimitiveType m_primitiveType;
        private int m_totalPrimitives;

        private BoundingBox m_box;

        public BoundingSphere Volume
        {
            get
            {
                return BoundingSphere.CreateFromBoundingBox(m_box);
            }
        }

        public BoundingBox Box
        {
            get
            {
                return m_box;
            }
        }

        public CVertexRenderObject(VertexPositionColor[] verts, GraphicsDevice device, PrimitiveType primitiveType)
        {
            m_verts = verts;

            m_vertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            m_primitiveType = primitiveType;

            Trace.Assert(m_primitiveType == PrimitiveType.TriangleList || m_primitiveType == PrimitiveType.LineList);

            int vertsPerPrimitive = (m_primitiveType == PrimitiveType.LineList) ? 2 : 3;

            m_totalPrimitives = m_verts.Length / vertsPerPrimitive;

            m_box.Min = m_box.Max = m_verts[0].Position;

            for (int i = 1; i < m_verts.Length; ++i)
            {
                m_box.Min = Vector3.Min(m_box.Min, m_verts[i].Position);
                m_box.Max = Vector3.Max(m_box.Max, m_verts[i].Position);
            }
        }

        public void Draw(GraphicsDevice device, Matrix m4World, Matrix m4View, Matrix m4Projection)
        {
            device.VertexDeclaration = m_vertexDeclaration;
            device.DrawUserPrimitives<VertexPositionColor>(m_primitiveType, m_verts, 0, m_totalPrimitives);
        }

        public void AddToBatch(CBatchManager batchManager, CRainbowXNA.CRenderInstance instance)
        {
            batchManager.Add(instance);
        }

        public CRainbowXNA.CRenderInstance CreateDefaultInstance(CRainbowXNA.RenderObjectHandleType handle, CRainbowXNA rainbow)
        {
            Trace.Assert(handle.RenderObject == this);
            //return rainbow.CreateRenderInstance(handle);
            return new CRainbowXNA.CRenderInstance(handle);
        }

    }
}

#endif
